#include "myjournal.h"
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QDir>
#include <QDebug>

MyJournal * MyJournal::obj = nullptr;

MyJournal::MyJournal(QObject *parent) : QObject(parent)
{
    //创建日志目录
    if(!QDir("Journal").exists())
    {
        QDir dir;
        dir.mkpath("Journal");
    }
}

MyJournal *MyJournal::getObject()
{
    if(obj == nullptr)
        obj = new MyJournal;
    return obj;
}

void MyJournal::printJournal(const QString &info)
{
    //1.打开文件
    QFile file(QString("Journal/info_%1.log")
        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
    bool ok = file.open(QIODevice::WriteOnly | QIODevice::Text
                        | QIODevice::Append);
    if(!ok)
        return;
    //2.创建流
    QTextStream stream(&file);
    stream << QString("[%1]")
              .arg(QDateTime::currentDateTime()
                   .toString("yyyy-MM-dd hh:mm:ss"))
           << info
           << endl;
    //3.关闭文件
    file.close();
}



